package com.odontosis.view.impressao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.odontosis.ioc.ContainerIoc;
import com.odontosis.service.UsuarioService;
import com.odontosis.view.OdontosisAction;

public class AcaoImpressao extends OdontosisAction<UsuarioService> {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormImpressao formulario = (FormImpressao) form;
		if(formulario.getMetodo().equals("finalizado")){
			return onExecutar(mapping, form, request, response);
		}
		return onIniciar(mapping, form, request, response);
		
	}
	
	public ActionForward onExecutar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormImpressao formulario = (FormImpressao) form;
		String end = formulario.getEnderecoImpressao();
		Collection<String> sucesso = new ArrayList<String>();
		sucesso.add("Recibo Gerado com sucesso.");
		request.setAttribute("sucesso", sucesso);
		request.setAttribute("mensagens", null);
		boolean reimpressao = (request.getSession().getAttribute("reimpressao") != null ?(Boolean) request.getSession().getAttribute("reimpressao") : false);
		if(reimpressao) {
			return mapping.findForward("listagemrecibo");
		}
		return mapping.findForward("pagamento");
	}
	
	public ActionForward onIniciar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormImpressao formulario = (FormImpressao) form;
		formulario.setEnderecoImpressao(ContainerIoc.getParametrosService().buscarEnderecoImpressora());
		List<String> textoImpressao = (List<String>) request.getSession().getAttribute("textoImpressao");
		formulario.setTexto(textoImpressao);
		
		return mapping.getInputForward();
	}
}
